package com.junmeng.jdemo.recycler.chat.viewbinding;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.base.BaseActivityDelegate;
import com.example.common.recycler.base.CommonRecyclerAdapter;
import com.example.common.recycler.viewbinding.ViewBindingBindViewManager2;
import com.junmeng.jdemo.databinding.ActivityChatDemoBinding;

public class ChatDemoActivity extends BaseActivityDelegate {
    ActivityChatDemoBinding binding;
    CommonRecyclerAdapter chatAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChatDemoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        binding.recyclerView.setAdapter(chatAdapter = new CommonRecyclerAdapter(new ViewBindingBindViewManager2()));
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
        chatAdapter.register(new BindViewText());
        chatAdapter.register(new BindViewImage());
        chatAdapter.register(new BindViewSystem());
        chatAdapter.setOnItemClickLitener((v, position, o) -> showToast("click position=" + position));
        chatAdapter.setOnItemLongClickListener((v, position, o) -> {
            showToast("long click position=" + position);
            return true;
        });

        binding.swipeRefreshLayout.setOnRefreshListener(() -> {
            binding.swipeRefreshLayout.postDelayed((Runnable) () -> {
                final int num = 10;
                for (int i = 0; i < num; i++) {
                    int rand = (int) (Math.random() * 3);
                    int kindRand = (int) (Math.random() * 2);
                    switch (rand) {
                        case 0:
                            chatAdapter.addItemToFront(new BindViewText.Item(kindRand, chatAdapter.getItemCount() + ""));
                            break;
                        case 1:
                            chatAdapter.addItemToFront(new BindViewImage.Item(kindRand, chatAdapter.getItemCount() + ""));
                            break;
                        case 2:
                            chatAdapter.addItemToFront(new BindViewSystem.Item("XXX 撤回了消息"));
                            break;
                    }
                }
                chatAdapter.notifyItemRangeInserted(0, num);
                binding.swipeRefreshLayout.setRefreshing(false);
            }, 1000);//模拟延时1秒

        });

    }

    public void onClickReceive(View view) {
        boolean isScrollToBottom = chatAdapter.isScrollToBottom(binding.recyclerView);
        int rand = (int) (Math.random() * 2);
        switch (rand) {
            case 0:
                chatAdapter.addItem(new BindViewText.Item(0, chatAdapter.getItemCount() + ""));
                break;
            case 1:
                chatAdapter.addItem(new BindViewImage.Item(0, chatAdapter.getItemCount() + ""));
                break;
        }
        chatAdapter.notifyItemInserted(chatAdapter.getItemCount() - 1);
        if (isScrollToBottom) {
            chatAdapter.smoothScrollToBottom(binding.recyclerView);
        }
    }

    public void onClickSend(View view) {
        int rand = (int) (Math.random() * 2);
        switch (rand) {
            case 0:
                chatAdapter.addItem(new BindViewText.Item(1, chatAdapter.getItemCount() + ""));
                break;
            case 1:
                chatAdapter.addItem(new BindViewImage.Item(1, chatAdapter.getItemCount() + ""));
                break;
        }
        chatAdapter.notifyItemInserted(chatAdapter.getItemCount() - 1);
        chatAdapter.smoothScrollToBottom(binding.recyclerView);
    }

    public void onClickBack(View view) {
        onBackPressed();
    }

    public void onClickSystem(View view) {
        boolean isScrollToBottom = chatAdapter.isScrollToBottom(binding.recyclerView);
        int rand = (int) (Math.random() * 3);
        switch (rand) {
            case 0:
                chatAdapter.addItem(new BindViewSystem.Item("XXX 撤回了消息"));
                break;
            case 1:
                chatAdapter.addItem(new BindViewSystem.Item("XXX 已加入，XXX 和 AAA , BBB 都不是好友关系，请提高防范意识，注意财产安全，谨防上当受骗"));
                break;
            case 2:
                chatAdapter.addItem(new BindViewSystem.Item("XXX 已被禁言"));
                break;
        }
        chatAdapter.notifyItemInserted(chatAdapter.getItemCount() - 1);
        if (isScrollToBottom) {
            chatAdapter.smoothScrollToBottom(binding.recyclerView);
        }
    }
}